package tx.algoritmiek.week1;

public class FactorialFormula implements BinomialFormula {

    public double executeFormula(double n, double k) {
        if(n < k || n < 0 || k < 0) {
            return 0;
        } else if(k <= 1) {
            return n;
        } else {
            return factorialCalc(n) / (factorialCalc(k) * factorialCalc(n-k));
        }
    }

    private double factorialCalc(double n) {
        double fac = 1;
        for(int i = 1; i <= n; i++)
            fac = fac * i;

        return fac;
    }
}
